/*
-------------------------------------------------------------------
BIE is Copyright 2001-2003 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
*/
/**
 *  Data bean representing a sbie configuration file.
 */
package com.webdeninteractive.sbie.config.data;

import java.io.File;
import java.lang.StringBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;

public class ConfigData {

    File upgradeDir;
    String bootsystemclass;
    String bootsystemname;
    ArrayList clients = new ArrayList();
    HashMap extraclasses = new HashMap(); // keys are classnames

    public boolean isValid() {
        return ( upgradeDir!=null &&
                 bootsystemclass!=null &&
                 bootsystemname!=null );
    }

    public void setUpgradeDir( String upgradeDir ) {
        this.upgradeDir = new File(upgradeDir);
    }

    public void setUpgradeDirAsFile( File udFile ) {
        this.upgradeDir = udFile;
    }

    // getUpgradeDir() must return the same type as setUpgradeDir() takes,
    // or Digester will be all CONFUSED
    public String getUpgradeDir() {
        return this.upgradeDir.getPath();
    }

    public File getUpgradeDirAsFile() {
        return this.upgradeDir;
    }

    public void setBootSystemClassName( String classname ) {
        this.bootsystemclass = classname;
    }

    public String getBootSystemClassName() {
        return this.bootsystemclass;
    }

    public void setBootSystemName( String name ) {
        this.bootsystemname = name;
    }

    public String getBootSystemName() {
        return this.bootsystemname;
    }

    public void addClient( ClientData client ) {
        clients.add(client);
    }

    /** @return a Collection of ClientData objects. */
    public Collection getClients() {
        return (Collection)clients;
    }

    public void clearClients() {
        clients = new ArrayList();
    }

    public void setExtraClass( String classname ) {
        extraclasses.put( classname, null );
    }

    public Collection getExtraClasses() {
        return (Collection)extraclasses.keySet();
    }

    public String toString() {
        StringBuffer ret = new StringBuffer();
        ret.append("<sbie-configuration>\n");
        String path = (upgradeDir!=null ? upgradeDir.getPath() : "");
        ret.append("<upgradeDir>" + path + "</upgradeDir>\n");
        ret.append("<bootsystem name=\"" + bootsystemname + "\">\n" );
        ret.append(" <class>" + bootsystemclass + "</class>\n" );
        ret.append("</bootsystem>\n");
        Iterator it;
        it = extraclasses.keySet().iterator();
        while( it.hasNext() ) {
            ret.append("<extraclass>" + (String)it.next() + "</extraclass>\n");
        }
        it = clients.iterator();
        while(it.hasNext()) {
            ret.append( it.next().toString() );
        }
        ret.append("</sbie-configuration>\n");
        return ret.toString();
    }
}

